package com.yiaik.app.controller;

import com.yiaik.app.service.UserService;

import com.yiaik.base.bean.ResultData;
import com.yiaik.base.bean.ResUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Value("${paging.page-size:20}")
    private String pageSize;

    @Value("${security.oauth2.client.client-id:null}")
    private String clientId;


    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("")
    public Authentication getUser(Authentication user) {
        return user;
    }

    @RequestMapping(value = "/client")
    public String client() {
        return clientId;
    }

    @RequestMapping(value = "/page")
    public String paging() {
        return pageSize;
    }


    @RequestMapping(value = "/login")
    public ResultData<ResUser> login(@RequestBody ResUser user) {
        log.info("before: {}", user);
        ResUser result = userService.login(user);
        log.info("after: {}", result);
        return ResultData.<ResUser>builder().data(result).build();
    }

    @RequestMapping(value = "/register")
    public ResultData<ResUser> register(@RequestBody ResUser user) {
        return ResultData.<ResUser>builder().data(userService.register(user)).build();
    }

}
